package Test1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;

public class Funcation {
	// 1,通过system. out提示信息,采用scanner录入学生信息,保存至集合。
	// 2,查看全部学生信息。
	// 3,按学生姓名查询该学生信息。
	// 4,创建学生类,记录保存至集合。
	// 5,添加专业,按专业查询学生信息

	static ArrayList<Student> studentArrayList = new ArrayList<>();
	static Scanner scanner = new Scanner(System.in);
 
	/*
	 * 初始化
	 */
    static JDBCUile jdbcUile = new JDBCUile(); 
    static Connection connection = jdbcUile.getConnection();

    
	
	

	public void addStudent() throws Exception {

		while (true) {

			System.out.println("请输入学生id");
			int id = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入学生姓名");
			String name = scanner.nextLine();
			System.out.println("请输入学生年龄");
			int age = Integer.parseInt(scanner.nextLine());
			System.out.println("请输入学生性别");
			String sex = scanner.nextLine();
			System.out.println("请输入学生班级"); 
			String classGrade = scanner.nextLine();
			System.out.println("请输入学生专业");
			String major = scanner.nextLine();
			
			Student student = new Student(id, name, age, sex, classGrade, major);
			
			jdbcUile.insertSingle(student);
						
			

			System.out.println("继续录入请输入'1',录入结束请输入'0'"); 
				

			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}

//		System.out.println(studentArrayList.get(0));

	}

	// 查看全部学生信息。

	public void findAll() throws Exception {
		int i = 1;
		
		studentArrayList = jdbcUile.selectAll();
		
		for (Student student : studentArrayList) {
			int id = student.getId();
			String name = student.getName();
			int age = student.getAge();
			String sex = student.getSex();
			String classGrade = student.getClass_grade();
			String major = student.getMajor();

			System.out.println(i + " 学号: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + sex + ", 班级: " + classGrade
					+ ", 专业=" + major);
			i++;

		}

	}

	// 按学生姓名查询该学生信息
	public void findbyName() throws Exception {
	    
		   
		while (true) {
			System.out.println("请输入学生姓名:");
			String name1 = scanner.nextLine();
			 
			Student student1 = new Student();
			student1.setName(name1);
			studentArrayList = jdbcUile.selectSingle(student1);				
			for (Student student : studentArrayList) {
					int id = student.getId();
					String name = student.getName();
					int age = student.getAge();
					String sex = student.getSex();
					String classGrade = student.getClass_grade();
					String major = student.getMajor();
					System.out.println(" 学号: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + sex + ", 班级 :"
							+ classGrade + "专业: " + major);
				}
			
			System.out.println("继续查询请输入1，结束查询请输入0");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}
	}

	// 添加学生专业
	public void addMajor() {
		
		while (true) {
			System.out.println("请输入要添加的学生的学号");
			int id = Integer.parseInt(scanner.nextLine());

			System.out.println("请输入该学生的专业");
			String major = scanner.nextLine();
			
			jdbcUile.updateInformation("major", major, id);
						

			System.out.println("继续添加请输入'1',结束添加请输入'0'");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}

		}
	
	}
 
	// 按照专业查询学生
	public void findbyMajor() throws Exception {
		
		
		while (true) {
			System.out.println("请输入学生专业:");
			String major1 = scanner.nextLine();
			Student student1 = new Student();
			student1.setMajor(major1);
			
			studentArrayList = jdbcUile.selectSingle(student1);
	
			for (Student student : studentArrayList) {
					int id = student.getId();
					String name = student.getName();
					int age = student.getAge();
					String sex = student.getSex();
					String classGrade = student.getClass_grade();
                    String major =student.getMajor();
					System.out.println(" 学号: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + sex + ", 班级: "
							+ classGrade + "专业: " + major);
				
			}
			System.out.println("继续查询请输入1，结束查询请输入0");
			int flag = Integer.parseInt(scanner.nextLine());
			if (flag == 0) {
				break;
			}
		}
		
	}

   //删除某学生信息    (批量删除，单个删除都可)
	public void deleteList() {
		while(true) {
			System.out.println("请输入要删除的学生的 id");
			int id = Integer.parseInt(scanner.nextLine());
			Student student1 = new Student();
			student1.setId(id);
			jdbcUile.delte(student1);
			System.out.println("是否要继续删除，输入yes/no");
			String flag = scanner.nextLine();
			if("no".equals(flag)) {
				break;
			}
			
			
		}
		
	}
	
	// 修改学生信息
		public void updateInfo() {
			
			while (true) {
				System.out.println("请输入要添加的学生的学号");
				int id = Integer.parseInt(scanner.nextLine());
                System.out.println("请输入要修改的内容");
				String clume = scanner.nextLine();
				System.out.println("请输入要修改的值");
				String value = scanner.nextLine();
				
				jdbcUile.updateInformation(clume, value, id);
							

				System.out.println("继续修改请输入'1',结束添加请输入'0'");
				int flag = Integer.parseInt(scanner.nextLine());
				if (flag == 0) {
					break;
				}

			}
		
		}

	// 菜单功能展示
	public void menuShow() {
		System.out.println("******** 1.添加学生信息        ********");
		System.out.println("******** 2.查找所有学生信息     ********");
		System.out.println("******** 3.输入姓名查找学生信息  ********");
		System.out.println("******** 4.添加学生专业信息     ********");
		System.out.println("******** 5.输入专业查找学生信息  ********");
		System.out.println("******** 6.删除学生信息 ********");
		System.out.println("******** 7.修改学生信息 ********");
		

	}

	// 菜单功能选择
	public void menuChoose() throws Exception {
		while (true) {
			menuShow();
			System.out.println("请输入你要选择的功能");
			int choose = Integer.parseInt(scanner.nextLine());
			switch (choose) {
			case 1: {
				addStudent();
				break;

			}
			case 2: {
				findAll();
				break;
			}
			case 3: {
				findbyName();
				break;
			}
			case 4: {
				addMajor();
				break;
			}
			case 5: {
				findbyMajor();
				break;
			}
			case 6: {
				deleteList();
				break;
			}
			case 7: {
				updateInfo();
				break;
			}
			default:
				throw new IllegalArgumentException("输入错误");
			}
			System.out.println("退出请输入'#',继续输入请输入任意");
			String flag = scanner.nextLine();
			if ("#".equals(flag)) {
				break;
			}
		}
	}
}
